using Sage.Http.Utilities;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

namespace Sage.Http.Factory
{
    /// <summary>
    /// JSON序列化工厂，提供统一的序列化/反序列化接口，支持AOT环境
    /// </summary>
    /// <remarks>
    /// 此类现在主要作为JsonSerializerUtilities的包装器，保持向后兼容性。
    /// 建议直接使用JsonSerializerUtilities和JsonSerializerOptionsFactory。
    /// </remarks>
    public static class JsonSerializerFactory
    {
        /// <summary>
        /// 获取全局JSON序列化选项
        /// </summary>
        /// <returns>全局JSON序列化选项，如果未设置则返回null</returns>
        public static JsonSerializerOptions? GetGlobalOptions()
        {
            return JsonSerializerOptionsFactory.GetGlobalOptions();
        }

        /// <summary>
        /// 设置全局JSON序列化选项
        /// </summary>
        /// <param name="options">要设置的JSON序列化选项</param>
        public static void SetGlobalOptions(JsonSerializerOptions? options)
        {
            JsonSerializerOptionsFactory.SetGlobalOptions(options);
        }

        /// <summary>
        /// 获取全局JSON序列化上下文
        /// </summary>
        /// <returns>全局JSON序列化上下文，如果未设置则返回null</returns>
        public static JsonSerializerContext? GetGlobalContext()
        {
            return JsonSerializerOptionsFactory.GetGlobalContext();
        }

        /// <summary>
        /// 设置全局JSON序列化上下文
        /// </summary>
        /// <param name="context">要设置的JSON序列化上下文</param>
        public static void SetGlobalContext(JsonSerializerContext? context)
        {
            JsonSerializerOptionsFactory.SetGlobalContext(context);
        }

        /// <summary>
        /// 获取有效的JSON序列化选项
        /// </summary>
        /// <param name="options">指定的选项</param>
        /// <returns>有效的JSON序列化选项</returns>
        public static JsonSerializerOptions GetOptions(JsonSerializerOptions? options = null)
        {
            return JsonSerializerOptionsFactory.GetEffectiveOptions(options);
        }

        /// <summary>
        /// 创建反序列化函数
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="jsonTypeInfo">JSON类型信息，用于AOT环境（推荐）</param>
        /// <param name="jsonOptions">自定义JSON序列化选项</param>
        /// <returns>反序列化函数</returns>
        /// <remarks>
        /// 在AOT环境中，建议提供jsonTypeInfo参数以启用源生成序列化。
        /// 如果同时提供了jsonTypeInfo和jsonOptions，将优先使用jsonTypeInfo。
        /// </remarks>
        [RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed.")]
        [RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation.")]
        public static Func<string, T> CreateDeserializer<T>(
            JsonTypeInfo<T>? jsonTypeInfo = null,
            JsonSerializerOptions? jsonOptions = null)
        {
            return JsonSerializerUtilities.CreateDeserializer(jsonTypeInfo, jsonOptions);
        }

        /// <summary>
        /// 尝试反序列化JSON字符串
        /// </summary>
        /// <typeparam name="T">目标类型</typeparam>
        /// <param name="json">JSON字符串</param>
        /// <param name="deserializer">反序列化函数</param>
        /// <param name="result">反序列化结果</param>
        /// <returns>如果反序列化成功返回true，否则返回false</returns>
        public static bool TryDeserialize<T>(
            string json,
            Func<string, T> deserializer,
            [NotNullWhen(true)] out T? result)
        {
            return JsonSerializerUtilities.TryDeserialize(json, deserializer, out result);
        }
    }
}